
 
         {qmfdeclre.i}
        
         define shared variable comp like qaddb.ps_mstr.ps_comp  .
         define shared variable t_site like qaddb.si_mstr.si_site.
         define shared variable site like qaddb.si_mstr.si_site.
         define shared variable site1 like qaddb.si_mstr.si_site.
       
         define shared variable maxlevel as integer no-undo.
         define shared var      eff_date as date.
         DEFINE NEW SHARED VARIABLE v_par LIKE qaddb.ps_mstr.ps_par . 
         define variable qty as decimal initial 1 no-undo.
         define variable level as integer initial 1 no-undo.
         define variable record as integer extent 1000 no-undo.
         define variable save_qty as decimal extent 1000 no-undo.
         define variable i as integer no-undo.
         define variable effstart as date no-undo.
         define variable effend as date no-undo.
         define variable eff_start as date extent 1000 no-undo.
         define variable eff_end as date extent 1000 no-undo.
         define buffer ptmstr for qaddb.pt_mstr.
         define variable par like qaddb.ps_mstr.ps_par.
         DEFINE VARIABLE lvla AS CHAR .

         DEFINE   SHARED TEMP-TABLE xps
                 FIELD xps_star     AS   CHAR  FORMAT "x(1)" 
                 FIELD xps_par      LIKE qaddb.pt_mstr.pt_part
                 FIELD xps_comp     LIKE qaddb.pt_mstr.pt_part
                 FIELD xps_pm       LIKE qaddb.pt_mstr.pt_pm_code   COLUMN-LABEL "P/M"
                 FIELD xps_lvl       AS  CHAR
                 FIELD xps_lvla       AS  CHAR
                 FIELD xps_start    LIKE qaddb.ps_mstr.ps_start
                 FIELD xps_end      LIKE qaddb.ps_mstr.ps_end
                 FIELD xps_ref      LIKE qaddb.ps_mstr.ps_ref
                 FIELD xps_op       LIKE qaddb.ps_mstr.ps_op
                 FIELD xps_qty      LIKE qaddb.ps_mstr.ps_qty_per
                 FIELD xps_ps_code  LIKE qaddb.ps_mstr.ps_ps_code
                 FIELD xps_scrp_pct LIKE qaddb.ps_mstr.ps_scrp_pct
                 FIELD xps_item_no  LIKE qaddb.ps_mstr.ps_item_no
                 FIELD xps_fcst_pct LIKE qaddb.ps_mstr.ps_fcst_pct
                 FIELD xps_rmks     LIKE qaddb.ps_mstr.ps_rmks
                 FIELD xps_process  LIKE qaddb.ps_mstr.ps_process
                 FIELD xps__chr01   LIKE qaddb.ps_mstr.ps__chr01
                 FIELD xps_lt_off   LIKE qaddb.ps_mstr.ps_lt_off
                 FIELD xps_group    LIKE qaddb.ps_mstr.ps_group
                 field xps_recid     as   recid .

         v_par = comp .
        
        {mfdel.i myqaddb.pk_det "where myqaddb.pk_det.pk_user = mfguser"} 
         FIND FIRST myqaddb.ps_mstr WHERE myqaddb.ps_mstr.ps_par = comp
              and (myqaddb.ps_mstr.ps_start = ? or (myqaddb.ps_mstr.ps_start <= eff_date and myqaddb.ps_mstr.ps_start <> ?))
              and (myqaddb.ps_mstr.ps_end   = ? or (myqaddb.ps_mstr.ps_end >= eff_date and myqaddb.ps_mstr.ps_end <> ?))
              no-lock no-error.
         
         repeat:
            if not available myqaddb.ps_mstr then do:
               REPEAT:
                   level = level - 1. 
                   if level < 1 then leave. 
                   find myqaddb.ps_mstr where recid(myqaddb.ps_mstr) = record[level]
                       no-lock no-error.
                    comp = myqaddb.ps_mstr.ps_par.
                    find next myqaddb.ps_mstr use-index ps_parcomp   where myqaddb.ps_mstr.ps_par = comp
                       and (myqaddb.ps_mstr.ps_start = ? or
                           (myqaddb.ps_mstr.ps_start <= eff_date and myqaddb.ps_mstr.ps_start <> ?))
                       and (myqaddb.ps_mstr.ps_end   = ? or
                           (myqaddb.ps_mstr.ps_end >= eff_date and myqaddb.ps_mstr.ps_end <> ?))
                       no-lock no-error.
                  if available myqaddb.ps_mstr then leave.

               END.
            END.
            if level < 1 then leave.
            par = myqaddb.ps_mstr.ps_par. 
            /*  Remark  : if the parent is "P" part, NOT continue
              explode the part */
 
            if (myqaddb.ps_mstr.ps_ps_code = "" or myqaddb.ps_mstr.ps_ps_code = "X")
            and myqaddb.ps_mstr.ps_qty_per <> 0 and level < 1000
            and (level < maxlevel or maxlevel = 0) then do:
 
               record[level] = recid(myqaddb.ps_mstr).
               save_qty[level] = qty.
               lvla = "........".
		       lvla = substring(lvla,1,min (level - 1,9)) + string(level).
		       if length(lvla) > 10
		       then lvla = substring(lvla,length (lvla) - 9,10). 
               
               eff_start[level] = max(effstart,myqaddb.ps_mstr.ps_start).
 
               comp = myqaddb.ps_mstr.ps_comp.
 
               find myqaddb.pt_mstr no-lock where myqaddb.pt_mstr.pt_part = ps_comp no-error.
               if available myqaddb.pt_mstr and myqaddb.pt_mstr.pt_bom_code <> "" then
                  comp = myqaddb.pt_mstr.pt_bom_code.
 
               FIND FIRST xps WHERE xps_par = myqaddb.ps_mstr.ps_par AND xps_comp = myqaddb.ps_mstr.ps_comp
                              AND xps_ref = myqaddb.ps_mstr.ps_ref
                              AND xps_start = myqaddb.ps_mstr.ps_start  EXCLUSIVE-LOCK NO-ERROR .  
               IF NOT AVAIL xps  THEN DO:
                  CREATE xps .
                  ASSIGN xps_par = myqaddb.ps_mstr.ps_par
                         xps_ref = myqaddb.ps_mstr.ps_ref
                         xps_comp = myqaddb.ps_mstr.ps_comp
                         xps_start = myqaddb.ps_mstr.ps_start
                         xps_pm    = myqaddb.pt_mstr.pt_pm_code 
                         xps_end   = myqaddb.ps_mstr.ps_end 
                         xps_qty   = myqaddb.ps_mstr.ps_qty_per 
                         xps_lvl  = string(level) 
                         xps_lvla  = lvla 
                         xps_op = myqaddb.ps_mstr.ps_op
                         xps_ps_code = myqaddb.ps_mstr.ps_ps_code
                         xps_scrp_pct  =  myqaddb.ps_mstr.ps_scrp_pct
                         xps_item_no =  myqaddb.ps_mstr.ps_item_no
                         xps_fcst_pct  =  myqaddb.ps_mstr.ps_fcst_pct 
                         xps_process =  myqaddb.ps_mstr.ps_process 
                         xps_rmks =  myqaddb.ps_mstr.ps_rmks 
                         xps__chr01  =  myqaddb.ps_mstr.ps__chr01 
                         xps_lt_off   =  myqaddb.ps_mstr.ps_lt_off
                         xps_group    =  myqaddb.ps_mstr.ps_group 
                         xps_recid = RECID(xps)  .


               END.
               level = level + 1.
               find first myqaddb.ps_mstr use-index ps_parcomp where myqaddb.ps_mstr.ps_par = comp
                       and (myqaddb.ps_mstr.ps_start = ? or
                           (myqaddb.ps_mstr.ps_start <= eff_date and myqaddb.ps_mstr.ps_start <> ?))
                       and (myqaddb.ps_mstr.ps_end   = ? or
                           (myqaddb.ps_mstr.ps_end >= eff_date and myqaddb.ps_mstr.ps_end <> ?))
                         no-lock no-error.
            END.
            else
            find next myqaddb.ps_mstr use-index ps_parcomp where myqaddb.ps_mstr.ps_par = comp
                       and (myqaddb.ps_mstr.ps_start = ? or
                           (myqaddb.ps_mstr.ps_start <= eff_date and myqaddb.ps_mstr.ps_start <> ?))
                       and (myqaddb.ps_mstr.ps_end   = ? or
                           (myqaddb.ps_mstr.ps_end >= eff_date and myqaddb.ps_mstr.ps_end <> ?))
 
            no-lock no-error.

            
         END.
